<template>
    <div class="choosemusic">
        <div class="header padding-24 space-between bg-active">
            <div class="h-back">
                <p class="fs-32 color-f" @click="cancel">取消</p>
            </div>
            <div class="title fs-36 color-f">选择音乐</div>
            <div class="h-right">
                <!-- <p class="fs-32 color-f" @click="sure">完成</p> -->
            </div>
        </div>
        <div class="search-box bg-f center">
            <div class="input">
                <input type="text" placeholder="搜索要分享的音乐" v-model="keyword" @input="search">
            </div>
        </div>
        <div class="music-list bg-f padding-24">
            <p class="title fs-36">{{title}}</p>
            <div class="list" v-if="musiclist[0]">
                <div class="list-item" v-for="(item,index) in musiclist" :key="index" @click="check(item)" :class="{current:item.music_id==checkedid}">
                    <p class="fs-32 overFlow">{{item.music_name}}</p>
                    <p class="fs-24 color-9 overFlow">{{item.singer_name}}-{{item.music_list_name}}</p>
                </div>
            </div>
            <div class="center" v-else>没有相关歌曲哦~</div>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            title:'最近播放',
            musiclist:[],
            keyword:'',
            checkedid:null,
        }
    },
    created(){
        //this.getmusic()
        this.getrecentlyPlayedMusic()
    },
    methods:{
        getmusic(){
            let url = this.$host1 + `home/getMusic`
            this.$axios.get(url,{
                params:{
                    keyword:this.keyword,
                }
            }).then(res => {
                console.log(res)
                //return
                if (res.status == 200) {
                let resData = res.data;
                if (resData.code == 200) {
                    this.musiclist=resData.data
                }else{
                    this.musiclist=[]
                    this.AlertWin(resData.message);
                }
                }
            }).catch(() => {
                this.musiclist=[]
                this.AlertWin("网络错误！请稍后重试！");
            });
        },
        getrecentlyPlayedMusic(){
            let url = this.$host1 + `home/dynamic/recentlyPlayedMusic`
            this.$axios.get(url).then(res => {
                console.log(res)
                //return
                if (res.status == 200) {
                let resData = res.data;
                if (resData.code == 200) {
                    this.musiclist=resData.data
                }else{
                    this.musiclist=[]
                    this.AlertWin(resData.message);
                }
                }
            }).catch(() => {
                this.musiclist=[]
                this.AlertWin("网络错误！请稍后重试！");
            });
        },
        search(){
            if(this.keyword==''){
                this.title='最近播放'
                this.musiclist=[]
            }else{
                this.getmusic()
                this.title='搜索结果'
            }
        },
        cancel(){
            sessionStorage.removeItem('checkedmusicid')
            sessionStorage.removeItem('checkedmusicname')
            this.$router.go(-1)
        },
        check(i){
            this.checkedid=i.music_id
            sessionStorage.setItem('checkedmusicid',this.checkedid)
            sessionStorage.setItem('checkedmusicname',i.music_name)
            setTimeout(()=>{
                this.$router.go(-1)
            },500)
        }
    }
}
</script>
<style lang="scss" scoped>
.choosemusic{
    width: 100%;
    //height: 100%;
    //position: absolute;
    padding-top: 1rem;
    box-sizing: border-box;
    background-color: #fff;
    .header{
        width: 100%;
        height: 1rem;
        
        .title{
            font-size: 0.36rem;
            font-weight: bold;
        }
      
      
    }
    .search-box{
        padding:0.2rem 0.63rem;
        box-sizing: border-box;
        height: 1rem;
        .input{
            width: 100%;
            height: 100%;
            border-radius: 0.3rem;
            padding:0.001rem 0.2rem;
            box-sizing: border-box;
            border: 0.01rem solid #e9e9e9; 
            input{
                width: 100%;
                height: 100%;
                text-align: center;
                font-size: 0.32rem;

                //background-color: greenyellow
            }
        }
    }
    .music-list{
        .title{
            line-height: 1rem;
        }
        .list{
            .list-item{
                height: 1.4rem;
                padding-top: 0.001rem;
                box-sizing: border-box;
                border-bottom: 0.01rem solid #e9e9e9;
                .fs-32{
                    margin-top: 0.2rem;
                }
                .fs-24{
                    margin-top: 0.1rem;
                }
            }
            .current{
                p{
                    color: #F44349
                }
            }
        }
        .center{
            height: 2rem;
        }
    }
}
</style>